package com.pft.web.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import java.io.File;

public final class IntentUtils {

    private IntentUtils() {

    }

    /**
     * 判断是否支持给定的intent
     */
    public static boolean isSupportedIntent(final Context context, final Intent intent) {
        if (intent == null) {
            return false;
        } else {
            final PackageManager packageManager = context.getPackageManager();
            return intent.resolveActivity(packageManager) != null;
        }
    }

    /**
     * 获取拨打电话的intent
     */
    public static Intent getCallIntent(final String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            throw new IllegalArgumentException();
        } else {
            return new Intent(Intent.ACTION_CALL, Uri.parse(String.format("tel:%s", phoneNumber)));
        }
    }

    /**
     * 获取输入电话的intent
     */
    public static Intent getDialIntent(final String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            throw new IllegalArgumentException();
        } else {
            return new Intent(Intent.ACTION_DIAL, Uri.parse(String.format("tel:%s", phoneNumber)));
        }
    }

    /**
     * 获取发送短信的intent
     */
    public static Intent getSendToIntent(final String phoneNumber, final String content) {
        if (TextUtils.isEmpty(phoneNumber)) {
            throw new IllegalArgumentException();
        } else {
            final Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(String.format("smsto:%s", phoneNumber)));
            intent.putExtra("sms_body", content);
            return intent;
        }
    }

    /**
     * 获取查看详情的intent
     */
    public static Intent getViewIntent(final String url) {
        if (TextUtils.isEmpty(url)) {
            throw new IllegalArgumentException();
        } else {
            return new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        }
    }

    /**
     * 获取多媒体文件扫描的intent
     */
    public static Intent getMediaScannerIntent(final String path) {
        if (TextUtils.isEmpty(path)) {
            throw new IllegalArgumentException();
        } else {
            return new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path)));
        }
    }

    /**
     * 获取文件选择的intent
     */
    public static Intent getPickIntent(final String type) {
        if (TextUtils.isEmpty(type)) {
            throw new IllegalArgumentException();
        } else {
            final Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setDataAndType(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, type);
            return intent;
        }
    }

    /**
     * 获取拍照的intent
     */
    public static Intent getCaptureIntent(final Uri uri) {
        if (uri == null) {
            throw new IllegalArgumentException();
        } else {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            return intent;
        }
    }

    /**
     * 将给定的intent包装为可选择的intent
     */
    public static Intent getChooserIntent(final Intent intent, final String title) {
        if (intent == null) {
            throw new IllegalArgumentException();
        } else {
            return Intent.createChooser(intent, title);
        }
    }

}
